package com.eh.proxy.remote.remote.state;

import com.eh.proxy.remote.remote.base.AbstractState;
import com.eh.proxy.remote.remote.model.GumballMachine;

import java.util.Random;

/**
 * Created by David on 2016/5/1.
 */
public class HasQuarterState extends AbstractState {
    private static final long serialVersionUID = 5880673634405817370L;
    private Random randomWinner = new Random(System.currentTimeMillis());

    public HasQuarterState(GumballMachine gumballMachine) {
        super(gumballMachine);
    }

    @Override
    public void ejectQuarter() {
        System.out.println("Quarter returned.");
        gumballMachine.setCurrentState(gumballMachine.getNoQuarterState());
    }

    @Override
    public boolean turnCrank() {
        int winner = randomWinner.nextInt(10);
        if (winner == 0 && gumballMachine.getCount() > 1) {
            gumballMachine.setCurrentState(gumballMachine.getWinnerState());
        } else {
            gumballMachine.setCurrentState(gumballMachine.getSoldState());
        }
        return true;
    }

    @Override
    public String toString() {
        return "等待转动手柄";
    }
}
